﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietThuongLMS.Utils;
using System.Resources;
using VietThuongLMS.ViewModels;
using VietThuongLMS.Services;
using DCS.ComLib.Config;
using VietThuongLMS.Areas.Admin.Models;
using System.Web.Security;
using System.Text;

/// <summary>
/// Author: NHDuc
/// </summary>
namespace VietThuongLMS.Controllers
{
    [HandleError]
    public class HomeController : BaseController
    {
        public ActionResult Index()
        {
            AlbumViewModel accountList = AlbumService.GetAlbumList(9999, 1);
            return View(accountList);
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult TinTuc()
        {
            int page = 1;
            if (Request["p"] != null)
            {
                page = int.Parse(Request["p"].ToString());
            }

            TinTucViewModel tintucList = TinTucService.GetTinTucList(int.Parse(ConfigReader.ReadString("ItemPerPage")), page);
            TinTucViewModel popularList = TinTucService.GetTinTucPopularList(5, 1);
            TinTucViewModel recentList = TinTucService.GetTinTucRecentList(5, 1);
            HinhAnhViewModel hinhAnhList = HinhAnhService.GetHinhAnhList(6, 1);
            LoaiTinTucViewModel loaiTinTucList = LoaiTinTucService.GetLoaiTinTucList(9999, 1);

            TinTucPageViewModel list = new TinTucPageViewModel(tintucList, popularList, recentList, hinhAnhList, loaiTinTucList);

            //phân trang - tintucList
            Pagination pagination = new Pagination(true, Url.Content("~"), "", "Home", "TinTuc", "?p=", tintucList.Total, page, int.Parse(ConfigReader.ReadString("ItemPerPage")), "Previous", "Next", "First", "End");
            string pageLinks = pagination.GetPageLinks();
            ViewData["pageLinks"] = pageLinks;
            //end phân trang

            return View(list);
        }

        public ActionResult TinTucThang(string id)
        {
            if (id.Length != 6)
                RedirectToRoute("TinTuc");

            int month = int.Parse(id.Substring(0, 2));
            int year = int.Parse(id.Substring(2, 4));

            ViewData["month"] = month.ToString() + "/" + year.ToString();

            int page = 1;
            if (Request["p"] != null)
            {
                page = int.Parse(Request["p"].ToString());
            }

            TinTucViewModel tintucList = TinTucService.GetTinTucByMonthList(month, year, int.Parse(ConfigReader.ReadString("ItemPerPage")), page);
            TinTucViewModel popularList = TinTucService.GetTinTucPopularList(5, 1);
            TinTucViewModel recentList = TinTucService.GetTinTucRecentList(5, 1);
            HinhAnhViewModel hinhAnhList = HinhAnhService.GetHinhAnhList(6, 1);

            TinTucPageViewModel list = new TinTucPageViewModel(tintucList, popularList, recentList, hinhAnhList);

            //phân trang - tintucList
            Pagination pagination = new Pagination(true, Url.Content("~"), "", "Home", "TinTucThang", "?tag=" + id + "p=", tintucList.Total, page, int.Parse(ConfigReader.ReadString("ItemPerPage")), "Previous", "Next", "First", "End");
            string pageLinks = pagination.GetPageLinks();
            ViewData["pageLinks"] = pageLinks;
            //end phân trang

            return View(list);
        }


        public ActionResult TinTucTheoLoai(int id)
        {
            int page = 1;
            if (Request["p"] != null)
            {
                page = int.Parse(Request["p"].ToString());
            }

            TinTucViewModel tintucList = TinTucService.GetTinTucList(int.Parse(ConfigReader.ReadString("ItemPerPage")), page, id);
            TinTucViewModel popularList = TinTucService.GetTinTucPopularList(5, 1);
            TinTucViewModel recentList = TinTucService.GetTinTucRecentList(5, 1);
            HinhAnhViewModel hinhAnhList = HinhAnhService.GetHinhAnhList(6, 1);
            LoaiTinTucViewModel loaiTinTucList = LoaiTinTucService.GetLoaiTinTucList(9999, 1);

            TinTucPageViewModel list = new TinTucPageViewModel(tintucList, popularList, recentList, hinhAnhList, loaiTinTucList);

            //phân trang - tintucList
            Pagination pagination = new Pagination(true, Url.Content("~"), "", "Home", "TinTuc", "?p=", tintucList.Total, page, int.Parse(ConfigReader.ReadString("ItemPerPage")), "Previous", "Next", "First", "End");
            string pageLinks = pagination.GetPageLinks();
            ViewData["pageLinks"] = pageLinks;
            //end phân trang

            return View(list);
        }

        public ActionResult TinTucDetail(int id)
        {
            //tang luot view len
            TinTucService.UpdateTinTucLuotView(id);
            //lay thong tin tin tuc ra de hien thi
            TinTuc tinTuc = TinTucService.GetTinTucById(id);
            return View(tinTuc);
        }

        public ActionResult Album()
        {
            int page = 1;
            if (Request["p"] != null)
            {
                page = int.Parse(Request["p"].ToString());
            }

            AlbumViewModel accountList = AlbumService.GetAlbumList(9999, page);

            //phân trang
            Pagination pagination = new Pagination(true, Url.Content("~"), "", "Home", "HinhAnh", "?p=", accountList.Total, page, int.Parse(ConfigReader.ReadString("ItemPerPage")), "Previous", "Next", "First", "End");
            string pageLinks = pagination.GetPageLinks();
            ViewData["pageLinks"] = pageLinks;
            //end phân trang

            return View(accountList);
        }

        public ActionResult AlbumSearch(string tag)
        {
            int page = 1;
            if (Request["p"] != null)
            {
                page = int.Parse(Request["p"].ToString());
            }

            AlbumViewModel accountList = AlbumService.GetAlbumByTagList(tag, int.Parse(ConfigReader.ReadString("ItemPerPage")), page);

            //phân trang
            Pagination pagination = new Pagination(true, Url.Content("~"), "", "Home", "HinhAnh", "?p=", accountList.Total, page, int.Parse(ConfigReader.ReadString("ItemPerPage")), "Previous", "Next", "First", "End");
            string pageLinks = pagination.GetPageLinks();
            ViewData["pageLinks"] = pageLinks;
            //end phân trang

            return View(accountList);
        }

        public ActionResult HoatDong(int id, int type)
        {
            int page = 1;
            if (Request["p"] != null)
            {
                page = int.Parse(Request["p"].ToString());
            }

            HinhAnhViewModel accountList = HinhAnhService.GetHinhAnhByAlbumID(id, type, 9999, page);

            //phân trang
            Pagination pagination = new Pagination(true, Url.Content("~"), "", "Home", "HinhAnh", "?p=", accountList.Total, page, 9999, "Previous", "Next", "First", "End");
            string pageLinks = pagination.GetPageLinks();
            ViewData["pageLinks"] = pageLinks;
            //end phân trang

            return View(accountList);
        }

        public ActionResult FAQ()
        {
            return View();
        }

        public ActionResult ChiNhanh()
        {
            int page = 1;
            if (Request["p"] != null)
            {
                page = int.Parse(Request["p"].ToString());
            }

            ChiNhanhViewModel ChiNhanhList = ChiNhanhService.GetChiNhanhList(int.Parse(ConfigReader.ReadString("ItemPerPage")), page);

            //phân trang
            Pagination pagination = new Pagination(true, Url.Content("~"), "Admin", "ChiNhanh", "Index", "?p=", ChiNhanhList.Total, page, int.Parse(ConfigReader.ReadString("ItemPerPage")), "Previous", "Next", "First", "End");
            string pageLinks = pagination.GetPageLinks();
            ViewData["pageLinks"] = pageLinks;
            //end phân trang

            return View(ChiNhanhList);
        }

        public ActionResult LienHe()
        {
            ChiNhanhViewModel ChiNhanhList = ChiNhanhService.GetChiNhanhList(10, 1);
            return View(ChiNhanhList);
        }

        [HttpPost]
        public ActionResult LienHeSumit(FormCollection form)
        {
            if (form["contact_name"].Equals("") || form["contact_email"].Equals("") || form["contact_message"].Equals(""))
                return View("LienHe");
            
            LienHe lh = new VietThuongLMS.LienHe();
            lh.HoTen = form["contact_name"].ToString();
            lh.Email = form["contact_email"].ToString();
            lh.DiaChi = form["contact_address"] != null ? form["contact_address"].ToString() : "";
            lh.DienThoai = form["contact_phone"] != null ? form["contact_phone"].ToString() : "";
            lh.NoiDung = form["contact_message"].ToString();
            lh.NgayDang = DateTime.Now;
            lh.Deleted = Constant.GENERAL_STATUS_NORMAL;

            LienHeService.InsertLienHe(lh);

            return View("LienHeSuccess");
        }

        public ActionResult LienHeSuccess()
        {
            return View();
        }

        public JsonResult GetChiNhanh()
        {
            return Json(ChiNhanhService.GetChiNhanhJson());
        }

        public ActionResult DangKy()
        {
            return View();
        }

        public ActionResult DangKyStep2(int id)
        {
            ViewData["LoaiLH"] = id;
            List<ChiNhanh> list = ChiNhanhService.GetChiNhanhList();
            StringBuilder temp = new StringBuilder();
            StringBuilder hinhThuc = new StringBuilder();
            bool isFirst = true;
            foreach (ChiNhanh cn in list)
            {
                if (isFirst)
                {
                    temp.Append("<input type=\"radio\" value=\"" + cn.ID + "\" name=\"ChiNhanh\" id=\"ChiNhanh\" class=\"ml15\" checked=\"checked\" >&nbsp;");
                    temp.Append(cn.TenChiNhanh);
                    temp.Append(" (" + cn.DiaChi + ") <br />");
                    isFirst = false;
                }
                else
                {
                    temp.Append("<input type=\"radio\" value=\"" + cn.ID + "\" name=\"ChiNhanh\" id=\"ChiNhanh\" class=\"ml15\" >&nbsp;");
                    temp.Append(cn.TenChiNhanh);
                    temp.Append(" (" + cn.DiaChi + ") <br />");
                }
            }

            //hinh thuc thanh toan
            hinhThuc.Append("<input type=\"radio\" value=\"1\" name=\"HinhThuc\" class=\"ml15\" checked=\"checked\" >&nbsp;");
            hinhThuc.Append("Tiền mặt");
            hinhThuc.Append("<br />");
            hinhThuc.Append("<input type=\"radio\" value=\"2\" name=\"HinhThuc\" class=\"ml15\" >&nbsp;");
            hinhThuc.Append("Chuyển khoản");
            hinhThuc.Append("<br />");

            ViewData["ChiNhanhList"] = temp.ToString();
            ViewData["HinhThucThanhToan"] = hinhThuc.ToString();
            return View();
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult DangKyStep2(DangKyModel model)
        {
            TaiKhoan tk = AccountService.GetAccount(model.MaTaiKhoan);
            if (tk != null && !tk.MaTaiKhoan.Equals(""))
            {
                ModelState.AddModelError("MaTaiKhoan", "(*) Tên đăng nhập đã tồn tại");
                return View(model);
            }

            ViewData["LoaiLH"] = model.LoaiLopHoc.ToString();
            if (ModelState.IsValid)
            {
                try
                {
                    //Tai khoan
                    TaiKhoan taiKhoan = new TaiKhoan();
                    taiKhoan.MaTaiKhoan = model.MaTaiKhoan;
                    taiKhoan.MatKhau = FormsAuthentication.HashPasswordForStoringInConfigFile(model.MatKhau, "md5");
                    taiKhoan.HoTen = model.HoTen;
                    taiKhoan.GioiTinh = model.GioiTinh;
                    taiKhoan.NgaySinh = (new Converter()).StringToDate(model.NgaySinh);
                    taiKhoan.NgheNghiep = model.NgheNghiep;
                    taiKhoan.NoiCongTac = model.NoiCongTac;
                    taiKhoan.Lop = model.Lop;
                    taiKhoan.DiaChi = model.DiaChi;
                    taiKhoan.DienThoai = model.DienThoai;
                    taiKhoan.Email = model.Email;
                    taiKhoan.NgayDangKy = DateTime.Now;
                    taiKhoan.Deleted = Constant.GENERAL_STATUS_NORMAL;
                    taiKhoan.MaLoaiTaiKhoan = Constant.ACCOUNT_ROLE_STUDENT;
                    
                    int taiKhoanId = AccountService.InsertAccountReturn(taiKhoan);

                    //Hoc vien
                    HocVien hocvien = new HocVien();
                    hocvien.HoTenLienHe = model.HoTenLienHe;
                    hocvien.MoiQuanHe = model.MoiQuanHe;
                    hocvien.DiaChiNguoiLienHe = model.DiaChiNguoiLienHe;
                    hocvien.DienThoaiNguoiLienHe = model.DienThoaiNguoiLienHe;
                    hocvien.EmailNguoiLienHe = model.EmailNguoiLienHe;
                    hocvien.MaTaiKhoan = taiKhoanId;

                    int hocVienId = HocVienService.InsertHocVienReturn(hocvien);

                    if (hocVienId != -1)
                    {
                        //Hoc vien - Lop
                        HocVien_Lop hvLop = new HocVien_Lop();
                        hvLop.NhacCu1 = model.NhacCu1 + "*@*" + model.SoNamHoc1 + "*@*" + model.GiaoVienHienTai1;
                        hvLop.NhacCu2 = model.NhacCu2 + "*@*" + model.SoNamHoc2 + "*@*" + model.GiaoVienHienTai2;
                        hvLop.DaBieuDien = model.DaBieuDien;
                        hvLop.ThamGiaBanNhac = model.ThamGiaBanNhac;
                        hvLop.BanNhac = (model.BanNhac != null) ? model.BanNhac : "";
                        hvLop.VaiTro = (model.VaiTro != null) ? model.VaiTro : "";
                        hvLop.SoThanhVien = (model.SoThanhVien != null) ? int.Parse(model.SoThanhVien) : 0;

                        hvLop.NgonNgu = (model.NgonNgu != null) ? model.NgonNgu : "";
                        hvLop.MaLoaiLopHoc = 1;
                        hvLop.NgayBatDauHoc = (new Converter()).StringToDate(model.NgayBatDauHoc);
                        hvLop.ThoiGianHoc = model.TGH_2 + "*@*" + model.TGH_3 + "*@*" + model.TGH_4 + "*@*" +
                                            model.TGH_5 + "*@*" + model.TGH_6 + "*@*" + model.TGH_7 + "*@*" + model.TGH_CN;
                        hvLop.ChuyenDe = model.CD_GuitarThung + "*@*" + model.CD_Violin + "*@*" + model.CD_Flute + "*@*"
                                       + model.CD_GuitarDien + "*@*" + model.CD_Viola + "*@*" + model.CD_ThanhNhac + "*@*"
                                       + model.CD_GuitarBass + "*@*" + model.CD_Clarinet + "*@*" + model.CD_Keyboard + "*@*"
                                       + model.CD_Piano + "*@*" + model.CD_Saxophone + "*@*" + model.CD_Trong + "*@*"
                                       + model.CD_NhacLy + "*@*" + model.CD_ChuyenDeKhac;

                        hvLop.BanNhacYeuThich = model.YT_CoDien + "*@*" + model.YT_Pop + "*@*" + model.YT_Jazz + "*@*"
                                              + model.YT_RB + "*@*" + model.YT_Blue + "*@*" + model.YT_Techno + "*@*"
                                              + model.YT_Dance + "*@*" + model.YT_Rock + "*@*" + model.YT_Khac;

                        hvLop.MongMuon = model.BD_DamDong + "*@*" + model.BD_HocChoVui + "*@*" + model.BD_ChuyenNghiep + "*@*"
                                       + model.BD_Khac;
                        hvLop.ThanTuong = (model.ThanTuong != null) ? model.ThanTuong : "";
                        hvLop.NgayDangKy = DateTime.Now;
                        hvLop.Deleted = Constant.GENERAL_STATUS_NORMAL;
                        hvLop.MaHocVien = hocVienId;
                        hvLop.DongHocPhi = Constant.HOCPHI_CHUADONG;
                        hvLop.MaChiNhanh = model.ChiNhanh;
                        hvLop.HinhThucThanhToan = model.HinhThuc;

                        bool result = HocVien_LopService.InsertHocVienLop(hvLop);
                        //////////

                        if (result)
                            return RedirectToAction("DangKyThanhCong", "Home");
                    }
                    return View(model);
                }
                catch (Exception ex)
                {

                }
            }
            List<ChiNhanh> list = ChiNhanhService.GetChiNhanhList();
            StringBuilder temp = new StringBuilder();
            bool isFirst = true;
            foreach (ChiNhanh cn in list)
            {
                if (isFirst)
                {
                    temp.Append("<input type=\"radio\" value=\"" + cn.ID + "\" name=\"ChiNhanh\" id=\"ChiNhanh\" class=\"ml15\" checked=\"checked\" >&nbsp;");
                    temp.Append(cn.TenChiNhanh);
                    temp.Append(" (" + cn.DiaChi + ") <br />");
                    isFirst = false;
                }
                else
                {
                    temp.Append("<input type=\"radio\" value=\"" + cn.ID + "\" name=\"ChiNhanh\" id=\"ChiNhanh\" class=\"ml15\" >&nbsp;");
                    temp.Append(cn.TenChiNhanh);
                    temp.Append(" (" + cn.DiaChi + ") <br />");
                }
            }
            ViewData["ChiNhanhList"] = temp.ToString();
            return View(model);
        }

        //Da ngon ngu
        public ActionResult SetCulture(String lang)
        {
            ResourcesMgt.SetLocalize(lang);
            Session["lang"] = lang;
            return Redirect(System.Web.VirtualPathUtility.ToAbsolute("~/" + Request.UrlReferrer.PathAndQuery));
        }

        public ActionResult DangNhap()
        {
            return View();
        }

        [HttpPost]
        public ActionResult DangNhap(LogOnModel model, string returnUrl)//FormCollection form)
        {
            if (ModelState.IsValid)
            {
                if (AccountService.SignIn(model.UserName, FormsAuthentication.HashPasswordForStoringInConfigFile(model.Password, "md5")))
                //if (true)
                {
                    if (String.IsNullOrEmpty(model.UserName)) throw new ArgumentException("Value cannot be null or empty.", "userName");

                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                    if (!String.IsNullOrEmpty(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Tên tài khoản hoặc mật khẩu không đúng.");
                }
            }

            // If we got this far, something failed, redisplay form
            return View();
        }

        public ActionResult Thoat()
        {
            FormsAuthentication.SignOut();

            return RedirectToAction("Index", "Home");
        }

        public ActionResult DangKyThanhCong()
        {
            return View();
        }

        public ActionResult ThongTinLop()
        {
            HocVien_LopViewModel hocVienList = HocVien_LopService.GetHocVienLopList(HttpContext.User.Identity.Name);
            foreach(HocVien_LopMoRongModel model in hocVienList.ListMoRong)
            {
                model.HV_L.ChuyenDe = (new Converter()).ChuyenDeParser(model.HV_L.ChuyenDe);
            }
                
            return View(hocVienList);
        }

        public ActionResult DangKyThem()
        {
            List<ChiNhanh> list = ChiNhanhService.GetChiNhanhList();
            StringBuilder temp = new StringBuilder();
            bool isFirst = true;
            foreach (ChiNhanh cn in list)
            {
                if (isFirst)
                {
                    temp.Append("<input type=\"radio\" value=\"" + cn.ID + "\" name=\"ChiNhanh\" id=\"ChiNhanh\" class=\"ml15\" checked=\"checked\" >&nbsp;");
                    temp.Append(cn.TenChiNhanh);
                    temp.Append(" (" + cn.DiaChi + ") <br />");
                    isFirst = false;
                }
                else
                {
                    temp.Append("<input type=\"radio\" value=\"" + cn.ID + "\" name=\"ChiNhanh\" id=\"ChiNhanh\" class=\"ml15\" >&nbsp;");
                    temp.Append(cn.TenChiNhanh);
                    temp.Append(" (" + cn.DiaChi + ") <br />");
                }
            }
            ViewData["ChiNhanhList"] = temp.ToString();
            return View();
        }

        [HttpPost]
        public ActionResult DangKyThem(DangKyThemModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    //Tai khoan
                    int taiKhoanId = AccountService.GetAccountIdByName(HttpContext.User.Identity.Name);

                    //Hoc vien
                    int hocVienId = HocVienService.GetHocVienIdByTaiKhoanId(taiKhoanId);

                    if (hocVienId != -1)
                    {
                        //Hoc vien - Lop
                        HocVien_Lop old_hvLop = HocVien_LopService.GetHocVienLopByHocVienId(hocVienId);
                        HocVien_Lop hvLop = new HocVien_Lop();
                        if (old_hvLop != null)
                        {
                            hvLop.NhacCu1 = old_hvLop.NhacCu1;
                            hvLop.NhacCu2 = old_hvLop.NhacCu2;
                            hvLop.DaBieuDien = old_hvLop.DaBieuDien;
                            hvLop.ThamGiaBanNhac = old_hvLop.ThamGiaBanNhac;
                            hvLop.BanNhac = old_hvLop.BanNhac;
                            hvLop.VaiTro = old_hvLop.VaiTro;
                            hvLop.SoThanhVien = old_hvLop.SoThanhVien;
                            hvLop.BanNhacYeuThich = old_hvLop.BanNhacYeuThich;
                            hvLop.MongMuon = old_hvLop.MongMuon;
                            hvLop.ThanTuong = old_hvLop.ThanTuong;
                        }
                        else
                        {
                            hvLop.NhacCu1 = "*@**@*";
                            hvLop.NhacCu2 = "*@**@*";
                            hvLop.DaBieuDien = 0;
                            hvLop.ThamGiaBanNhac = 0;
                            hvLop.BanNhac = "";
                            hvLop.VaiTro = "";
                            hvLop.SoThanhVien = 0;
                            hvLop.BanNhacYeuThich = "False*@*False*@*False*@*False*@*False*@*False*@*False*@*False*@*False";
                            hvLop.MongMuon = "False*@*False*@*False*@*False";
                            hvLop.ThanTuong = "";
                        }

                        hvLop.NgonNgu = (model.NgonNgu != null) ? model.NgonNgu : "";
                        hvLop.MaLoaiLopHoc = 1;
                        hvLop.NgayBatDauHoc = (new Converter()).StringToDate(model.NgayBatDauHoc);
                        hvLop.ThoiGianHoc = model.TGH_2 + "*@*" + model.TGH_3 + "*@*" + model.TGH_4 + "*@*" +
                                            model.TGH_5 + "*@*" + model.TGH_6 + "*@*" + model.TGH_7 + "*@*" + model.TGH_CN;
                        hvLop.ChuyenDe = model.CD_GuitarThung + "*@*" + model.CD_Violin + "*@*" + model.CD_Flute + "*@*"
                                       + model.CD_GuitarDien + "*@*" + model.CD_Viola + "*@*" + model.CD_ThanhNhac + "*@*"
                                       + model.CD_GuitarBass + "*@*" + model.CD_Clarinet + "*@*" + model.CD_Keyboard + "*@*"
                                       + model.CD_Piano + "*@*" + model.CD_Saxophone + "*@*" + model.CD_Trong + "*@*"
                                       + model.CD_NhacLy + "*@*" + model.CD_ChuyenDeKhac;

                        hvLop.NgayDangKy = DateTime.Now;
                        hvLop.Deleted = Constant.GENERAL_STATUS_NORMAL;
                        hvLop.MaHocVien = hocVienId;
                        hvLop.DongHocPhi = Constant.HOCPHI_CHUADONG;
                        hvLop.MaChiNhanh = model.ChiNhanh;
                        hvLop.HinhThucThanhToan = model.HinhThuc;

                        bool result = HocVien_LopService.InsertHocVienLop(hvLop);
                        //////////

                        if (result)
                            return RedirectToAction("DangKyThanhCong", "Home");
                    }
                    return View(model);
                }
                catch (Exception ex)
                {

                }
            }
            return View(model);
        }

        public ActionResult GioiThieu()
        {
            return View();
        }

        public ActionResult GiaoVien()
        {
            GiaoVienByMonHocViewModel giaoVienList = GiaoVienService.GetGiaoVienByMonHocList();
            return View(giaoVienList);
        }

        public ActionResult ThongTinGiaoVien(int id)
        {
            GiaoVien giaoVien = GiaoVienService.GetGiaoVienById(id);
            return View(giaoVien);
        }

        public ActionResult ThongTinHocVien()
        {
            ThongTinHocVienModel hocvien = HocVienService.GetThongTinHocVien(HttpContext.User.Identity.Name);
            return View(hocvien);
        }

        public ActionResult EditThongTinHocVien(int id)
        {
            HocVien hv = HocVienService.GetHocVienById(id);
            CapNhatHocVienModel model = new CapNhatHocVienModel();
            model.id = hv.ID.ToString();
            model.HoTen = hv.TaiKhoan.HoTen != null ? hv.TaiKhoan.HoTen : "";
            model.DiaChi = hv.TaiKhoan.DiaChi != null ? hv.TaiKhoan.DiaChi : "";
            model.DienThoai = hv.TaiKhoan.DienThoai != null ? hv.TaiKhoan.DienThoai : "";
            model.NgaySinh = hv.TaiKhoan.NgaySinh != null ? hv.TaiKhoan.NgaySinh.Value.ToString("dd/MM/yyyy") : "";
            model.NgheNghiep = hv.TaiKhoan.NgheNghiep != null ? hv.TaiKhoan.NgheNghiep : "";
            model.NoiCongTac = hv.TaiKhoan.NoiCongTac != null ? hv.TaiKhoan.NoiCongTac : "";

            model.HoTenLienHe = hv.HoTenLienHe != null ? hv.HoTenLienHe : "";
            model.MoiQuanHe = hv.MoiQuanHe != null ? hv.MoiQuanHe : "";
            model.DiaChiNguoiLienHe = hv.DiaChiNguoiLienHe != null ? hv.DiaChiNguoiLienHe : "";
            model.DienThoaiNguoiLienHe = hv.DienThoaiNguoiLienHe != null ? hv.DienThoaiNguoiLienHe : "";
            model.EmailNguoiLienHe = hv.EmailNguoiLienHe != null ? hv.EmailNguoiLienHe : "";
            return View(model);
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult EditThongTinHocVienSubmit(CapNhatHocVienModel collection)
        {
            HocVien hv = HocVienService.GetHocVienById(int.Parse(collection.id));
            CapNhatHocVienModel model = new CapNhatHocVienModel();
            model.id = hv.ID.ToString();
            model.HoTen = hv.TaiKhoan.HoTen != null ? hv.TaiKhoan.HoTen : "";
            model.DiaChi = hv.TaiKhoan.DiaChi != null ? hv.TaiKhoan.DiaChi : "";
            model.DienThoai = hv.TaiKhoan.DienThoai != null ? hv.TaiKhoan.DienThoai : "";
            model.NgaySinh = hv.TaiKhoan.NgaySinh != null ? hv.TaiKhoan.NgaySinh.Value.ToString("dd/MM/yyyy") : "";
            model.NgheNghiep = hv.TaiKhoan.NgheNghiep != null ? hv.TaiKhoan.NgheNghiep : "";
            model.NoiCongTac = hv.TaiKhoan.NoiCongTac != null ? hv.TaiKhoan.NoiCongTac : "";

            model.HoTenLienHe = hv.HoTenLienHe != null ? hv.HoTenLienHe : "";
            model.MoiQuanHe = hv.MoiQuanHe != null ? hv.MoiQuanHe : "";
            model.DiaChiNguoiLienHe = hv.DiaChiNguoiLienHe != null ? hv.DiaChiNguoiLienHe : "";
            model.DienThoaiNguoiLienHe = hv.DienThoaiNguoiLienHe != null ? hv.DienThoaiNguoiLienHe : "";
            model.EmailNguoiLienHe = hv.EmailNguoiLienHe != null ? hv.EmailNguoiLienHe : "";

            bool flag = true;

            if (collection.HoTen == null || collection.HoTen.Equals(""))
            {
                ModelState.AddModelError("HoTen", "(*) Họ tên không được để trống");
                flag = false;
            }
            if (collection.DiaChi == null || collection.DiaChi.Equals(""))
            {
                ModelState.AddModelError("DiaChi", "(*) Địa chỉ không được để trống");
                flag = false;
            }
            if (collection.DienThoai == null || collection.DienThoai.Equals(""))
            {
                ModelState.AddModelError("DienThoai", "(*) Điện thoại không được để trống");
                flag = false;
            }

            if (collection.HoTenLienHe == null || collection.HoTenLienHe.Equals(""))
            {
                ModelState.AddModelError("HoTenLienHe", "(*) Họ tên người liên hệ không được để trống");
                flag = false;
            }
            if (collection.MoiQuanHe == null || collection.MoiQuanHe.Equals(""))
            {
                ModelState.AddModelError("MoiQuanHe", "(*) Mối quan hệ không được để trống");
                flag = false;
            }
            if (collection.DiaChiNguoiLienHe == null || collection.DiaChiNguoiLienHe.Equals(""))
            {
                ModelState.AddModelError("DiaChiNguoiLienHe", "(*) Địa chỉ người liên hệ không được để trống");
                flag = false;
            }
            if (collection.DienThoaiNguoiLienHe == null || collection.DienThoaiNguoiLienHe.Equals(""))
            {
                ModelState.AddModelError("DienThoaiNguoiLienHe", "(*) Điện thoại người liên hệ không được để trống");
                flag = false;
            }
            if (collection.EmailNguoiLienHe == null || collection.EmailNguoiLienHe.Equals(""))
            {
                ModelState.AddModelError("EmailNguoiLienHe", "(*) Email người liên hệ không được để trống");
                flag = false;
            }

            if (flag)
            {
                try
                {
                    TaiKhoan tk = new TaiKhoan();

                    tk.HoTen = collection.HoTen;
                    tk.DiaChi = collection.DiaChi;
                    tk.DienThoai = collection.DienThoai;
                    tk.Email = collection.Email;
                    Converter c = new Converter();
                    tk.NgaySinh = c.StringToDate(collection.NgaySinh);

                    tk.ID = (int)hv.MaTaiKhoan;
                    tk.NgheNghiep = collection.NgheNghiep;
                    tk.NoiCongTac = collection.NoiCongTac;

                    //Ok
                    AccountService.Edit(tk);

                    hv.HoTenLienHe = collection.HoTenLienHe;
                    hv.MoiQuanHe = collection.MoiQuanHe;
                    hv.DiaChiNguoiLienHe = collection.DiaChiNguoiLienHe;
                    hv.DienThoaiNguoiLienHe = collection.DienThoaiNguoiLienHe;
                    hv.EmailNguoiLienHe = collection.EmailNguoiLienHe;

                    //Ok
                    HocVienService.Edit(hv);

                    //return View(hv);
                    //}
                    return RedirectToAction("ThongTinHocVien");
                }
                catch (Exception ex)
                {
                    DCS.ComLib.File.Log.Write("HocVienController::Edit", ex.StackTrace, ex.Message);
                    return View();
                }
            }
            //Invalid, redisplay form with errors
            return View("EditThongTinHocVien", model);
        }

        public ActionResult DiemDanh()
        {
            //lay danh sach lop
            List<LopHocMini> lopList = LopHocService.GetDanhSachLopByTaiKhoanId(HttpContext.User.Identity.Name);

            //ung voi moi lop, lay danh sach diem danh
            foreach (LopHocMini lop in lopList)
            {
                lop.BangDD = DiemDanhService.GetDiemDanhByMaLopList(lop.ID, lop.MaHocVien);
            }

            return View(lopList);
        }

        public string CheckName(string username)
        {
            TaiKhoan tk = AccountService.GetAccount(username);
            if (tk != null && !tk.MaTaiKhoan.Equals(""))
                return "0";
            return "1";
        }

        public ActionResult DoiMatKhau()
        {
            return View();
        }

        [HttpPost]
        public ActionResult DoiMatKhauSubmit(ChangePassModel model)
        {
            if (!AccountService.SignIn(HttpContext.User.Identity.Name, FormsAuthentication.HashPasswordForStoringInConfigFile(model.OldPass, "md5")))
            {
                ModelState.AddModelError("OldPass", "(*) Mật khẩu cũ không đúng");
                return View("DoiMatKhau");
            }
            if (model.NewPass != model.RetypePass)
            {
                ModelState.AddModelError("NewPass", "(*) Mật khẩu và mật khẩu xác nhận không giống nhau");
                return View("DoiMatKhau");
            }
            bool result = AccountService.ChangePass(HttpContext.User.Identity.Name, FormsAuthentication.HashPasswordForStoringInConfigFile(model.NewPass, "md5"));
            if(result)
                return View("DoiMatKhauSubmit");
            return View("DoiMatKhau");
        }

        public ActionResult KhoaHoc()
        {
            return View();
        }
    }
}
